package com.lj.ljminsu.controller;

import com.lj.ljminsu.bean.JsonResultVo;
import com.lj.ljminsu.bean.TjUser;
import com.lj.ljminsu.service.IUserDaoService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 登录
 */
@RestController
@RequestMapping("user")
public class LoginController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserDaoService iUserDaoService;

    @CrossOrigin
    @RequestMapping("login")
    public JsonResultVo toLogin(String userName,String password,String verifyCode) {
        JsonResultVo jsonResultVo = new JsonResultVo();
        String text = stringRedisTemplate.opsForValue().get("text");
        Pattern p = Pattern.compile("1\\d{10}");
        Matcher m = p.matcher(userName);
        boolean b = m.matches();
        if (verifyCode.equalsIgnoreCase(text)) {
            if (userName.endsWith(".com")) {
                TjUser existTjUser = iUserDaoService.selectUserByUserEmail(userName);
                if (existTjUser==null||"".equals(existTjUser)) {
                    jsonResultVo.setCode(1);
                    jsonResultVo.setMsg("账户或密码错误");
                    return jsonResultVo;
                }else {
                    String existTjUserPassword = existTjUser.getPassword();
                    String existUserPhone = existTjUser.getUserPhone();
                    if (existTjUserPassword.equals(password)) {
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("role","1");
                        //登录成功之后，需要生成token
                        String token = Jwts.builder().setSubject(existUserPhone) //主题，可以放用户的详细信息
                                .setIssuedAt(new Date()) //token创建时间
                                .setExpiration(new Date(System.currentTimeMillis() + 259200000)) //token过期时间
                                .setId(String.valueOf(existTjUser.getUserId())) //用户ID
                                .addClaims(hashMap)
                                .signWith(SignatureAlgorithm.HS256, "qianfeng") //加密方式和加密密码
                                .compact();
                        jsonResultVo.setCode(0);
                        jsonResultVo.setMsg(token);
                        return  jsonResultVo;
                    }
                    jsonResultVo.setCode(1);
                    jsonResultVo.setMsg("账户或密码错误");
                    return  jsonResultVo;
                }
            }else if (b){
                TjUser existTjUser = iUserDaoService.selectUserByUserPhone(userName);
                if (existTjUser==null||"".equals(existTjUser)) {
                    jsonResultVo.setCode(1);
                    jsonResultVo.setMsg("账户或密码错误");
                    return jsonResultVo;
                }else {
                    String existTjUserPassword = existTjUser.getPassword();
                    String existUserPhone = existTjUser.getUserPhone();
                    if (existTjUserPassword.equals(password)) {
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("role","1");
                        //登录成功之后，需要生成token
                        String token = Jwts.builder().setSubject(existUserPhone) //主题，可以放用户的详细信息
                                .setIssuedAt(new Date()) //token创建时间
                                .setExpiration(new Date(System.currentTimeMillis() + 259200000)) //token过期时间
                                .setId(String.valueOf(existTjUser.getUserId())) //用户ID
                                .addClaims(hashMap)
                                .signWith(SignatureAlgorithm.HS256, "qianfeng") //加密方式和加密密码
                                .compact();
                        jsonResultVo.setCode(0);
                        jsonResultVo.setMsg(token);
                        return  jsonResultVo;
                    }
                    jsonResultVo.setCode(1);
                    jsonResultVo.setMsg("账户或密码错误");
                    return  jsonResultVo;
                }
            }else {
                TjUser existTjUser = iUserDaoService.selectUserByUserAccount(userName);
                if (existTjUser==null||"".equals(existTjUser)) {
                    jsonResultVo.setCode(1);
                    jsonResultVo.setMsg("账户或密码错误");
                    return jsonResultVo;
                }else {
                    String existTjUserPassword = existTjUser.getPassword();
                    String existUserPhone = existTjUser.getUserPhone();
                    if (existTjUserPassword.equals(password)) {
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("role","1");
                        //登录成功之后，需要生成token
                        String token = Jwts.builder().setSubject(existUserPhone) //主题，可以放用户的详细信息
                                .setIssuedAt(new Date()) //token创建时间
                                .setExpiration(new Date(System.currentTimeMillis() + 259200000)) //token过期时间
                                .setId(String.valueOf(existTjUser.getUserId())) //用户ID
                                .addClaims(hashMap)
                                .signWith(SignatureAlgorithm.HS256, "qianfeng") //加密方式和加密密码
                                .compact();
                        jsonResultVo.setCode(0);
                        jsonResultVo.setMsg(token);
                        return  jsonResultVo;
                    }
                    jsonResultVo.setCode(1);
                    jsonResultVo.setMsg("账户或密码错误");
                    return  jsonResultVo;
                }
            }
        }
        jsonResultVo.setCode(2);
        jsonResultVo.setMsg("验证码错误");
        return jsonResultVo;
    }

    @CrossOrigin
    @RequestMapping("phoneCode")
    public JsonResultVo toPhoneLogin(String smsCode,String userPhone,String smsVerifyCode) {
        JsonResultVo jsonResultVo = new JsonResultVo();
        String existUserPhone = iUserDaoService.checkPhone(userPhone);
        TjUser tjUser = iUserDaoService.selectUserByUserPhone(userPhone);
        String text = stringRedisTemplate.opsForValue().get("text");
        if (!smsVerifyCode.equalsIgnoreCase(text)) {
            jsonResultVo.setCode(3);
            jsonResultVo.setMsg("图形验证码错误");
            return jsonResultVo;
    }
        if (existUserPhone==null||"".equals(existUserPhone)) {
            jsonResultVo.setCode(1);
            jsonResultVo.setMsg("该手机号未注册，请注册");
            return jsonResultVo;
        }
        String phoneSecurityCode = stringRedisTemplate.opsForValue().get(userPhone + "phoneSecurityCode");
        if (phoneSecurityCode.equals(smsCode)) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("role","1");
            //登录成功之后，需要生成token
            String token = Jwts.builder().setSubject(existUserPhone) //主题，可以放用户的详细信息
                    .setIssuedAt(new Date()) //token创建时间
                    .setExpiration(new Date(System.currentTimeMillis() + 259200000)) //token过期时间
                    .setId(String.valueOf(tjUser.getUserId())) //用户ID
                    .addClaims(hashMap)
                    .signWith(SignatureAlgorithm.HS256, "qianfeng") //加密方式和加密密码
                    .compact();
            jsonResultVo.setCode(0);
            jsonResultVo.setMsg(token);
            return jsonResultVo;
        }
        jsonResultVo.setCode(2);
        jsonResultVo.setMsg("手机验证码错误！");
        return jsonResultVo;
    }
}
